/*
 *	This file is part of Transdroid <http://www.transdroid.org>
 *
 *	Transdroid is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Transdroid is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with Transdroid.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package org.transdroid.daemon;

/**
 * A class that contains all the settings for a server daemon to connect.
 *
 * @author erickok
 */
public final class DaemonSettings {

    private static final String DEFAULT_NAME = "Default";

    final private String name;
    final private Daemon type;
    final private String address;
    final private int port;
    final private boolean ssl;
    final private boolean sslTrustAll;
    final private String sslTrustKey;
    final private String folder;
    final private boolean useAuthentication;
    final private String username;
    final private String password;
    final private String extraPass;
    final private OS os;
    final private String downloadDir;
    final private String ftpUrl;
    final private String ftpPassword;
    final private int timeout;
    final private boolean alarmOnFinishedDownload;
    final private boolean alarmOnNewTorrent;
    final private String idString;
    final private boolean isAutoGenerated;

    protected DaemonSettings() {
        this(null, null, null, 0, false, false, null, null, false, null, null, null,
                null, null, null, null, 0, false, false, null, false);
    }

    /**
     * Creates a daemon settings instance, providing full connection details
     *
     * @param name              A name used to identify this server to the user
     * @param type              The server daemon type
     * @param address           The server domain name or IP address
     * @param port              The port on which the server daemon is running
     * @param sslTrustKey       The specific key that will be accepted.
     * @param folder            The server (SCGI) folder
     * @param useAuthentication Whether to use basic authentication
     * @param username          The user name to provide during authentication
     * @param password          The password to provide during authentication
     * @param extraPass         The Deluge web interface password
     * @param downloadDir       The default download directory (which may also be used as base directory for file paths)
     * @param ftpUrl            The partial URL to connect to when requesting FTP-style transfers
     * @param timeout           The number of seconds to wait before timing out a connection attempt
     * @param idString          The (numeric) identifier for this server settings (used as postfix on stored preferenced)
     * @param isAutoGenerated   Whether this setting was generated rather than manually inputed by the user
     */
    public DaemonSettings(String name, Daemon type, String address, int port, boolean ssl,
                          boolean sslTrustAll, String sslTrustKey, String folder, boolean useAuthentication,
                          String username, String password, String extraPass, OS os, String downloadDir, String ftpUrl, String ftpPassword, int timeout,
                          boolean alarmOnFinishedDownload, boolean alarmOnNewTorrent, String idString, boolean isAutoGenerated) {
        this.name = name;
        this.type = type;
        this.address = address;
        this.port = port;
        this.ssl = ssl;
        this.sslTrustAll = sslTrustAll;
        this.sslTrustKey = sslTrustKey;
        this.folder = folder;
        this.useAuthentication = useAuthentication;
        this.username = username;
        this.password = password;
        this.extraPass = extraPass;
        this.os = os;
        this.downloadDir = downloadDir;
        this.ftpUrl = ftpUrl;
        this.ftpPassword = ftpPassword;
        this.timeout = timeout;
        this.alarmOnFinishedDownload = alarmOnFinishedDownload;
        this.alarmOnNewTorrent = alarmOnNewTorrent;
        this.idString = idString;
        this.isAutoGenerated = isAutoGenerated;
    }

    public String getName() {
        return (name == null || name.equals("") ? DEFAULT_NAME : name);
    }

    public Daemon getType() {
        return type;
    }

    public String getAddress() {
        return address;
    }

    public int getPort() {
        return port;
    }

    public boolean getSsl() {
        return ssl;
    }

    public boolean getSslTrustAll() {
        return sslTrustAll;
    }

    public String getSslTrustKey() {
        return sslTrustKey;
    }

    public String getFolder() {
        return folder == null ? null : (folder.startsWith("/") ? folder : folder + "/");
    }

    public boolean shouldUseAuthentication() {
        return useAuthentication;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getExtraPassword() {
        return extraPass;
    }

    public OS getOS() {
        return os;
    }

    public String getDownloadDir() {
        return downloadDir;
    }

    public String getFtpUrl() {
        return ftpUrl;
    }

    public String getFtpPassword() {
        return ftpPassword;
    }

    public int getTimeoutInMilliseconds() {
        return timeout * 1000;
    }

    public boolean shouldAlarmOnFinishedDownload() {
        return alarmOnFinishedDownload;
    }

    public boolean shouldAlarmOnNewTorrent() {
        return alarmOnNewTorrent;
    }

    public String getIdString() {
        return idString;
    }

    public boolean isAutoGenerated() {
        return isAutoGenerated;
    }

    /**
     * Builds a text that can be used by a human reader to identify this daemon settings
     *
     * @return A concatenation of username, address, port and folder, where applicable
     */
    public String getHumanReadableIdentifier() {
        if (isAutoGenerated) {
            // Hide the 'implementation details'; just give the username and server
            return (this.shouldUseAuthentication() && this.getUsername() != null && !this.getUsername().equals("") ? this
                    .getUsername() + "@" : "") + getAddress();
        }
        return (this.ssl ? "https://" : "http://")
                + (this.shouldUseAuthentication() && this.getUsername() != null && !this.getUsername().equals("") ? this
                .getUsername() + "@" : "") + getAddress() + ":" + getPort()
                + (Daemon.supportsCustomFolder(getType()) && getFolder() != null ? getFolder() : "");
    }

    @Override
    public String toString() {
        return getHumanReadableIdentifier();
    }

}
